package com.xyy.saas.payment.merchant.core.service;

import com.xyy.saas.payment.callback.core.dto.CallbackMsg;
import com.xyy.saas.payment.callback.core.result.CallbackRunner;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.enums.AccountCreateStatusEnum;
import com.xyy.saas.payment.cores.enums.BusinessTypeEnum;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.cores.vo.CallbackResultVo;
import com.xyy.saas.payment.dao.model.PayAccount;
import com.xyy.saas.payment.merchant.core.dto.AccountOpenNoticeDto;
import com.xyy.saas.payment.merchant.core.vo.UserInfoVo;
import com.xyy.saas.payment.service.PayAccountService;
import com.xyy.saas.payment.util.DateUtil;
import com.xyy.saas.payment.util.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
@Slf4j
public class AccountOpenService {
    @Resource
    private UserInfoService2 userInfoService2;
    @Resource
    private CallbackRunner callbackRunner;
    @Resource
    private PayAccountService payAccountService;

    /**
     * 处理账户审核结果
     */
    public void dealAuditResult(AccountOpenNoticeDto noticeDto) throws Exception {
        log.info("AccountOpenService#dealAuditResult start. AccountOpenNoticeDto:{}", JSONUtils.toJSON(noticeDto));
        UserInfoVo userInfoVo = userInfoService2.queryUserInfo(noticeDto.getCustMerchantNo());

        if(userInfoVo == null) {
            throw new PaymentException(ResultCodeEnum.USER_NOT_FOUND);
        }

        // 1.更新状态
        AccountCreateStatusEnum auditStatus = noticeDto.auditPass() ? AccountCreateStatusEnum.PASS : AccountCreateStatusEnum.REJECT;
        userInfoVo.setAuditStatus(auditStatus.getStatus());
        userInfoVo.setMessage(noticeDto.auditPass() ? auditStatus.getDesc() : noticeDto.getMsg());
        userInfoVo.setUtime(DateUtil.now());
        userInfoService2.updateUserInfo(userInfoVo);

        // 2.通知业务
        String type = noticeDto.auditPass() ? CallbackResultVo.ACCOUNT_OPEN_AUDIT_PASS : CallbackResultVo.ACCOUNT_OPEN_AUDIT_REJECT;
        PayAccount payAccount = payAccountService.getPayAccountByAccountId(noticeDto.getCustMerchantNo());
        String callback = userInfoVo.getSettleInfo().getCallbackAddress();

        // 兼容历史数据user_info无callback情况
        if(StringUtils.isBlank(callback) && payAccount.getBusinessIdType().contains(BusinessTypeEnum.YIKUAIQIAN.getType())) {
            // TODO 待补充回调地址
            callback = "ykq_bank_card_audit_callback";
        }
        else if(StringUtils.isBlank(callback) && payAccount.getBusinessIdType().contains(BusinessTypeEnum.EC.getType())) {
            callback = "openAccountCallback";
        }

        String message = noticeDto.auditPass() ? auditStatus.getDesc() : noticeDto.getMsg();
        callbackRunner.callback(CallbackMsg.build(type, payAccount.getBusinessUserId(), callback, message));
    }
}
